package com.koron.css2.baseConfig.impl;

import com.koron.css2.ServerInterface;
import com.koron.css2.baseConfig.mapper.CostMapper;
import com.koron.css2.baseConfig.vo.CostVO;
import com.koron.css2.serviceManage.bean.RequestBean;
import com.koron.css2.serviceManage.bean.UserInfoBean;
import com.koron.util.Constant;
import com.koron.util.Tools;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.koron.ebs.mybatis.SessionFactory;
import org.swan.bean.MessageBean;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
/**
 * 费用名称-列表初始化
 * @author 周新宇
 */
public class CostList implements ServerInterface  {
	
	private static Logger logger = LogManager.getLogger(CostList.class);

	@Override
	public MessageBean<?> exec(SessionFactory factory, UserInfoBean userInfo, RequestBean req) {
		
		@SuppressWarnings("rawtypes")
		MessageBean<List> info = MessageBean.create(Constant.MESSAGE_INT_SUCCESS, "success", List.class);

		try {
			//先查询公共库的费用类型
			CostMapper commonMapper = factory.getMapper(CostMapper.class, "_default");
			List<CostVO> list = commonMapper.selectPubCostList(null);
			//水司库费用类型
 			CostMapper mapper = factory.getMapper(CostMapper.class);
			List<CostVO> comlist = mapper.selectCostList();
			List<String> costIds = comlist.stream().map(CostVO::getId).collect(Collectors.toList());
			//过滤出水司需要的费用类型
			List<CostVO> filterCostList = list.stream().filter(dto -> {
				String id = dto.getId();
				return costIds.contains(id);
			}).collect(Collectors.toList());

			Map<String,String> cou = Tools.mapDicByCode(factory, "COU");
 			//设置已经启用的费用类型
 			for(CostVO vo :filterCostList) {					//运维同步到水司
 				vo.setCostUnit(cou.get(vo.getCostUnit()));
 				vo.setUsed(false);
				if("1".equals(vo.getStatus())) {
					vo.setUsed(true);
 				}
 			}
			info.setData(filterCostList);
		} catch (Exception e) {
			info.setCode(Constant.MESSAGE_INT_FAIL);
			info.setDescription(e.getMessage());
			logger.error(e.getMessage(), e);
		}
		return info;
	}

}
